package com.seeyon.apps.u8cloud.manager;

import com.seeyon.apps.u8cloud.model.VoucherParams;
import com.seeyon.apps.u8cloud.model.VoucherResult;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.util.json.JSONUtil;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.logging.Log;
import www.seeyon.com.utils.MD5Util;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @ClassName : U8CloudApiManagerImpl
 * @Description :
 * @CreateDate : 2025/8/18 12:48
 * @Author : CheungHowrAn
 * @Version : 1.0.0
 * @PackageName com.seeyon.apps.u8cloud.manager
 * @Project : BSiEE本涩_A8_V9.0SP1
 */
public class U8CloudApiManagerImpl implements U8CloudApiManager {
    private static Log log = CtpLogFactory.getLog(U8CloudApiManager.class);
    private static String serviceUrl = AppContext.getSystemProperty("u8cloud.serviceUrl");
    private static String userName = AppContext.getSystemProperty("u8cloud.userName");
    private static String password = AppContext.getSystemProperty("u8cloud.password");

    /**
     * @MethodName: createVoucher
     * @Description: 创建凭证
     * @CreateDate: 2025/8/19 10:39
     * @Author: CheungHowrAn
     * @Param voucherParams
     * @Return VoucherResult
     */
    public VoucherResult createVoucher(VoucherParams voucherParams) {
        String url = serviceUrl + "/u8cloud/api/gl/voucher/insert";
        log.info("创建创建凭证URL：" + url);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
        map.put("system", "OA"); // 系统编码
        map.put("usercode", userName); // 用户
        map.put("password", MD5Util.MD5(password)); // 密码，需要 MD5 加密后录入
        log.info("创建创建凭证Header：" + JSONUtil.toJSONString(map));
        log.info("创建创建凭证参数：" + JSONUtil.toJSONString(voucherParams));
        String resultJson = client(url, map, JSONUtil.toJSONString(voucherParams));
        log.info("创建创建凭证返回值：" + resultJson);
        VoucherResult result = JSONUtil.parseJSONString(resultJson, VoucherResult.class);
        return result;

    }

    /**
     * @MethodName: client
     * @Description: httpClient
     * @CreateDate: 2025/8/18 15:42
     * @Author: CheungHowrAn
     * @Param url
     * @Param headers
     * @Param json
     * @Return String
     */
    private String client(String url, Map<String, Object> headers, String json) {
        try {
            HttpClient httpClient = new HttpClient();
            PostMethod httpPost = new PostMethod(url);
            httpPost.setRequestHeader("content-type", "application/json;charset=utf-8");
            for (Entry<String, Object> entry : headers.entrySet()) {
                httpPost.setRequestHeader(entry.getKey(), entry.getValue().toString());
            }
            RequestEntity entity = new StringRequestEntity(json, "application/json", "UTF-8");
            httpPost.setRequestEntity(entity);
            httpClient.executeMethod(httpPost);
            return httpPost.getResponseBodyAsString();
        } catch (Exception e) {
            log.error("调用U8Cloud接口异常:", e);
            VoucherResult voucherResult = new VoucherResult();
            voucherResult.setStatus("false");
            voucherResult.setErrormsg("调用U8C接口异常:" + e.getMessage());
            return JSONUtil.toJSONString(voucherResult);
        }
    }
}
